# Copyright 2009 Lee Harr
#
# This file is part of Acromania.
#
# Acromania is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Acromania is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Acromania.  If not, see <http://www.gnu.org/licenses/>.


from twisted.protocols import basic
from twisted.internet import reactor
later = reactor.callLater

import amgame
import amplayer
import colors
import amdb


class AM(basic.LineReceiver, amgame.CMix):
    'Twisted protocol. One is created for each client connection.'

    delimiter = '\n'

    def connectionMade(self):
        'A new connection. Send out the MOTD.'

        print "Got new client!"
        player = self.gameserver.join(self)
        self.player = player
        self.motd()

    def connectionLost(self, reason):
        'Client has disconnected.'

        print "Lost a client!"
        self.gameserver.leave(self.player)

    def leave(self):
        self.transport.loseConnection()

    def lineReceived(self, line):
        '''Called each time a new line of input is received from the client.
        '''

        line = line.strip()
        #print "received", repr(line)
        if not line:
            return
        elif line.startswith('/'):
            self.command(line[1:], self.player)
        else:
            msg = '%s: %s' % (self.player.name, line)
            self.gameserver.broadcast(msg)

    def change_name(self, player, name):
        if amdb.exists(name) and not player.username==name:
            player.message('Name "%s" is reserved.' % name)
            player.message('Login with /login <name> <password> if that is your account.')
            return

        if name not in self.gameserver.game.playernames():
            orig = player.name
            player.name = name
            broadcast = self.gameserver.broadcast
            broadcast('Player "%s" is now known as "%s"' % (orig, name))
        else:
            player.message('Name "%s" already in use.' % name)

    def motd(self):
        'Message of the day.'

        lines = open('MOTD').readlines()

        for line in lines:
            self.message(line.rstrip())

    def help(self, player):
        'Show HELP file.'

        lines = open('HELP.sa').readlines()

        for line in lines:
            self.message(line.rstrip())

    def rules(self, player):
        'Show RULES file.'

        lines = open('RULES').readlines()

        for line in lines:
            player.message(line.rstrip())

    def simessage(self, msg=''):
        'Send simple line to client. Used before player has logged in.'

        self.transport.write(msg + '\r\n')

    def message(self, *args, **kw):
        color = kw.get('color', True)
        strs = map(str, args)
        msg = ' '.join(strs)
        msg = msg % colors.yes
        self.transport.write(msg + '\r\n')

    def game_over(self, player):
        player.message('Game over. Type /new to start again.')
        player.message()
